//
//  HLCollectionViewController.m
//  YueYue
//
//  Created by hulu001 on 2023/5/17.
//

#import "HLCollectionViewController.h"

@interface HLCollectionViewController ()

@end

@implementation HLCollectionViewController

- (void)dealloc {
    _collectionView.dataSource = nil;
    _collectionView.delegate = nil;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.datasources = [[NSMutableArray alloc] init];
    if (self.collectionView.backgroundColor) {
        self.view.backgroundColor = self.collectionView.backgroundColor;
    }
}

- (void)initSubviews {
    [super initSubviews];
    [self initCollectionView];
}


- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [self layoutCollectionView];
    
    [self layoutEmptyView];
}

- (void)layoutCollectionView {
    BOOL shouldChangeTableViewFrame = !CGRectEqualToRect(self.view.bounds, self.collectionView.frame);
    if (shouldChangeTableViewFrame) {
        self.collectionView.qmui_frameApplyTransform = self.view.bounds;
    }
}


#pragma mark - 工具方法

@synthesize collectionView = _collectionView;
- (__kindof UICollectionView *)collectionView {
    if (!_collectionView) {
        [self loadViewIfNeeded];
    }
    return _collectionView;
}

- (void)setCollectionView:(__kindof UICollectionView *)collectionView {
    if (_collectionView != collectionView) {
        if (_collectionView) {
            if (self.isViewLoaded && _collectionView.superview == self.view) {
                [_collectionView removeFromSuperview];
            }
        }
        
        _collectionView = collectionView;
    
        if (collectionView.superview && self.nibName && !self.isViewLoaded) {
        } else {
            // 触发 loadView
            [self.view addSubview:_collectionView];
        }
    }
}



- (void)initCollectionView {
   if (!_collectionView) {
       UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
       self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout: layout];
       _collectionView.dataSource = self;
       _collectionView.delegate = self;
       _collectionView.showsVerticalScrollIndicator = NO;
       _collectionView.showsHorizontalScrollIndicator = NO;
   }
}

- (void)noMordDataWithFooterRefresh
{
    [self.collectionView.mj_footer endRefreshingWithNoMoreData];
}

- (void)setupMJRefresh
{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    [header setTitle:@"" forState:MJRefreshStateIdle];
    self.collectionView.mj_header = header;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    self.collectionView.mj_footer = footer;
}


- (BOOL)shouldCustomizeNavigationBarTransitionIfHideable {
    return YES;
}

- (void)headerRefresh
{
    [self.request start];
}

- (void)footerRefresh
{
    [self.request start];
}

- (void)requestFinished:(__kindof YTKBaseRequest *)request
{
    [self endAnimateRefresh];
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request
{
    [QMUITips showWithText:[request.error localizedDescription]];
    [self endAnimateRefresh];
}

- (void)endAnimateRefresh
{
    if ([self.collectionView.mj_header isRefreshing]){
        [self.collectionView.mj_header endRefreshing];
    }
    
    if ([self.collectionView.mj_footer isRefreshing]){
        [self.collectionView.mj_footer endRefreshing];
    }
}


- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return [[UICollectionViewCell alloc] init];
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 0;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

@end
